Python Numpy 矩阵更新混淆答案

您所在的位置:网站首页 python intersect1d Python Numpy 矩阵更新混淆答案

Python Numpy 矩阵更新混淆答案

2023-04-12 01:53| 来源: 网络整理| 查看: 265

这是我的矩阵分解代码的一部分(一个非常奇怪的 nmf 版本)。我的问题是,虽然每次迭代时,我都会保存 W 和 H 矩阵的旧副本,但当我在 W 每次完成更新后比较 old_W 和 W 时,它们实际上是相同的!所以实际的错误输出总是 0 并且 while 循环在第一次迭代后停止。但是,“#print old - new”显示元素 W[r][i] 实际上每次都会更新。什么是我没有看到的?

def csmf(V, l, max_iter, err, alpha=0.01, beta=0.01, lamb=0.01): W = np.random.rand(V.shape[0], l) H = np.random.rand(l, V.shape[1]) n = V.shape[0] N = V.shape[1] NwOone = 60 NwOtwo = 60 NhOone = 50 NhOtwo = 50 for t in range(max_iter): old_W = W # save old values old_H = H old = criterion(V,old_W,old_H,l,alpha,beta,lamb) print "iteration ", t ##### update W print "updating W" setw = range(0,n) subset_one = random.sample(setw,NwOone) subset_two = calcGw(V, W, H, n, l, alpha, beta, NwOtwo) chosen = np.intersect1d(subset_one,subset_two) for r in chosen: for i in range(len(W[0])): update = wPosNeg(W[r],N,i,l,V,r,beta,H) old = W[r][i] W[r][i] = update new = W[r][i] #print old - new ##### update H print "updating H" seth = range(0,N) subset_oneh = random.sample(seth,NhOone) subset_twoh = calcGh(V, W, H, N, l, NhOtwo,lamb) chosenh = np.intersect1d(subset_oneh,subset_twoh) for s in chosenh: # column for i in range(len(H)): updateh = hPosNeg(H[i],n,i,l,V,s,lamb,W) H[i][s] = updateh ##### check err print "Checking criterion" print criterion(V,W,H,l,alpha,beta,lamb) print criterion(V,old_W,old_H,l,alpha,beta,lamb) actual = abs(criterion(V,W,H,l,alpha,beta,lamb) -criterion(V,old_W,old_H,l,alpha,beta,lamb)) if actual


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3